﻿using Common;
using DbControl;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CentralMonitoring
{
    /// <summary>
    /// 预警规则配置窗体
    /// </summary>
    public partial class RuleConfigEdit : Form
    {    
        public int m_Id = -1;
        public string m_beforWarnids = "";
        private devices m_devices;
        public RuleConfigEdit()
        {
            InitializeComponent();
        }      
        private void buttonOK_Click(object sender, EventArgs e)
        {

            string strIds = "";
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["check"];
                if (Convert.ToBoolean(checkCell.Value) == true)
                {
                    strIds += dataGridView1.Rows[i].Cells["Id"].Value.ToString() + ",";
                }
            }
            if (strIds != "")
            {
                strIds = strIds.Substring(0, strIds.Length - 1);
                if (OprDeviceAlarmConfig.SetRules(m_Id, strIds))
                {
                    MessageBox.Show("规则设置成功！", "提示", MessageBoxButtons.OK);
                    DialogResult = DialogResult.OK;

                }
            }
            else
            {
                if (!string.IsNullOrEmpty(m_beforWarnids))
                {
                    if (OprDeviceAlarmConfig.ClearRules(m_Id))
                    {
                        MessageBox.Show("规则解除成功！", "提示", MessageBoxButtons.OK);
                        DialogResult = DialogResult.OK;
                    }
                }
                
            }         
           
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DeviceConfigEdit_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn();
            columncb.HeaderText = "选中";
            columncb.Name = "check";
            columncb.DataPropertyName = "IsChecked";
            columncb.TrueValue = true;
            columncb.FalseValue = false;
            this.dataGridView1.Columns.Add(columncb);
            if (m_Id > 0)
            {
                m_devices = OprDevice.GetdevicesById(m_Id);
                if (m_devices != null)
                {
                    
                    textBoxCode.Text = m_devices.device_code;
                    textBoxName.Text = m_devices.device_name;
                    textBoxSite.Text = m_devices.device_position;
                    if (m_devices.oil_id.HasValue)
                    {
                        oils oil = OprOil.GetOilById(m_devices.oil_id.Value);
                        textBoxOil.Text = oil.oil_name;                       
                    }
                    if(m_devices.warn_ids!=null)
                    {
                        m_beforWarnids = m_devices.warn_ids;
                    }
                    DataTable dataTable = OprDeviceAlarmConfig.GetAllRules();
                    this.dataGridView1.DataSource= dataTable;
                    dataGridView1.Columns["id"].Visible = false;
                    dataGridView1.Columns["rule_code"].HeaderText = "规则名称";
                    dataGridView1.Columns["sensor_id"].Visible = false;
                    dataGridView1.Columns["parameter_id"].Visible = false;
                    dataGridView1.Columns["parameter_name"].HeaderText = "指标名称";
                    dataGridView1.Columns["upper_upper_value"].HeaderText = "上上限";
                    dataGridView1.Columns["upper_value"].HeaderText = "上限";
                    dataGridView1.Columns["lower_value"].HeaderText = "下限";
                    dataGridView1.Columns["lower_lower_value"].HeaderText = "下下限";
                    dataGridView1.Columns["flag"].HeaderText = "状态";  
                    dgSetView.SetFormatDataGridView(dataGridView1);
                    if (string.IsNullOrEmpty(m_beforWarnids))
                        return;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {  
                        object obj_id = dataGridView1.Rows[i].Cells["id"].Value;
                        if (obj_id != null)
                        {
                            if(m_devices.warn_ids.Contains(obj_id.ToString()))
                                this.dataGridView1.Rows[i].Cells[0].Value = "true";
                        }
                    }
                }
            } 
           
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells["check"];
                Boolean flag = Convert.ToBoolean(checkCell.Value);
                if (flag)
                    checkCell.Value = false;
                else
                    checkCell.Value = true;

            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            bool misCheckedvalue = false;
            if (e.RowIndex == -1)
            {
                if (dataGridView1.Columns["check"].HeaderText == "全选")
                {
                    misCheckedvalue = false;
                    dataGridView1.Columns["check"].HeaderText = "全消";
                }
                else
                {
                    misCheckedvalue = true;
                    dataGridView1.Columns["check"].HeaderText = "全选";
                }
                int i;
                for (i = 0; i < this.dataGridView1.RowCount; i++)
                {
                    if (misCheckedvalue)
                        this.dataGridView1.Rows[i].Cells[0].Value = "false";
                    else
                        this.dataGridView1.Rows[i].Cells[0].Value = "true";

                }

            }
        }
    }
}
